/*生成项目信息组件
 * @Author: duanguang 
 * @Date: 2020-08-01 14:40:03 
 * @Last Modified by: duanguang
 * @Last Modified time: 2020-10-29 17:39:05
 */

import React from 'react';
import { LegionsProForm } from 'legions-pro-design';
import { ProjectFormFields,ProjectFormFieldsRule } from '../../models/form/projectFormFields';
import { bind,observer } from 'legions/store-react';
import ProjectStore from '../../stores/projectStore';
import { IHLFormUtils } from '../../components/property.config/formBasePropertyConfig';

interface IProps {
    store?: ProjectStore;
    controls: (that?: CreateProjectModule) => Array<IHLFormUtils['componentModel']>
}
/** 创建项目仓库信息表单组件 */
@bind({ store: ProjectStore })
@observer
export default class CreateProjectModule extends React.Component<IProps> {
    constructor(props: IProps) {
        super(props)
    }
    createProjectFormConfig() {
        return this.props.controls && this.props.controls(this)
    }
    render() {
        const store = this.props.store;
        return (
            <LegionsProForm
                <ProjectFormFields>
                InputDataModel={ProjectFormFields}
                mapPropsToFields={(props) => {
                    return new ProjectFormFields(props)
                }}
                onFieldsChange={(props,formFields) => {
                    this.props.store.createProjectFormRef.store.updateFormInputData(this.props.store.createProjectFormRef.uid,formFields);
                    if (this.props.store.projectEditViewModel.computedLoad === 'complete') {
                        this.props.store.projectEditViewModel.updateLoadingState('none');
                    }
                }}
                onReady={(form,ref) => {
                    
                    this.props.store.createProjectFormRef = { ...ref,that: this };
                    const projectConfig = {
                        init: ['modulesName','projectSrc','gitUserName','gitPwd'],
                        update:['componentName','branch'],
                    }
                    if (store.projectEditViewModel.createProjectType === 'update') {
                        projectConfig['init'].map((item) => {
                            store.createProjectFormRef.methods.setFormStates(item,(value) => {
                                value.iFormProps['disabled'] = true;
                            });
                        });
                        projectConfig.update.map((item) => {
                            store.createProjectFormRef.methods.setFormStates(item,(value) => {
                                value.iFormProps.visible = true;
                            });
                        })
                    }
                    if (store.projectEditViewModel.createProjectType === 'init') {
                        projectConfig[store.projectEditViewModel.createProjectType].map((item) => {
                            store.createProjectFormRef.methods.setFormStates(item,(value) => {
                                value.iFormProps['disabled'] = false;
                            });
                        });
                        projectConfig['update'].map((item) => {
                            store.createProjectFormRef.methods.setFormStates(item,(value) => {
                                value.iFormProps.visible = false;
                            });
                        })
                    }
                }}
                colCount={1}
                controls={this.createProjectFormConfig()}
            ></LegionsProForm>
        )
    }
}
